package com.lechuang.kit;

import com.lechuang.controller.Test;
import com.lechuang.model.User;
import com.lechuang.vo.BaseResult;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.UUID;

/**
 * Created by Administrator on 2016/9/29.
 */
public class Commons {

    public static String getUUID(){
        return UUID.randomUUID().toString();
    }
    public static void filter(Object o,String... fields){
        if(o == null){
            BaseResult.addError("1008","过滤对象不能为空");
        }
        try {

            for(String item:fields){
                Class<?> clazz = o.getClass();
                Field field = clazz.getDeclaredField(item);
                Class<?> declaringClass = field.getType();
                String method = getMethod(item);
                Method declaredMethod = clazz.getDeclaredMethod(method, declaringClass);
                declaredMethod.invoke(o,clazz.cast(null));
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }
    private static  String getMethod(String method){
        return "set"+method.substring(0,1).toUpperCase()+method.substring(1);
    }

    public static void main(String[] args){
        User user = new User();
        user.setPassword("123456");
        user.setNickName("Self Down");
        user.setIntegral(0);
        user.setRankId(0);
        filter(user,"password","nickName");
    }





}


